package com.fridge.shopping;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;

import com.fridge.data.DBHelper;
import com.fridge.data.ShoppingItem;
import com.fridge.fridgeList.AddFridgeItemActivity;
import com.id2216.fridge.R;

public class AddShoppingItemActivity extends Activity {

	
	EditText editNameText;
	NumberPicker quantityPicker;
	Button doneButton;
	Button cancelButton;
	private DBHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_shopping);
		
		
		editNameText = (EditText) this.findViewById(R.id.editShoppingName);
		quantityPicker = (NumberPicker) this.findViewById(R.id.quantityPicker);
		doneButton =(Button) this.findViewById(R.id.doneAddShopping);		
		cancelButton =(Button) this.findViewById(R.id.cancelAddShopping);
		
		quantityPicker.setValue(1);
		quantityPicker.setMinValue(1);
		quantityPicker.setMaxValue(30);
		
		
		this.addAction();
	}
	
	private void addAction()
	{
		cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	AddShoppingItemActivity.this.finish();
            }});
		
		doneButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(editNameText.getText().length() > 0)
            	{
            		
                    db = new DBHelper(getApplicationContext());                    
                    String name = editNameText.getText().toString();                    
                    ShoppingItem item = new ShoppingItem();
                    item.setName(name);
                    item.setQuantity(quantityPicker.getValue());                    
                    db.addShoppingItem(item);                    
                    db.closeDB();
                    
                    AddShoppingItemActivity.this.finish();

            	}
            	else
            	{
	                
	                AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddShoppingItemActivity.this);
	                alertDialog.setMessage("Name is empty");
	                alertDialog.setMessage("Please fill item name");
	                alertDialog.setCancelable(false);
	                alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                           public void onClick(DialogInterface dialog, int id) {
	                                //do things
	                           }
	                       });
	                AlertDialog alert = alertDialog.create();
	                alert.show();
            	}
            }
        });
	}
}

